import cv2

#获取第0个摄像头
cap = cv2.VideoCapture(0)
while True:
    ret, frame = cap.read()    #读取

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # 灰度处理
    face_cascade = cv2.CascadeClassifier('data/haarcascades/haarcascade_frontalface_default.xml')  # 使用训练模型加载定位脸位置

    faces = face_cascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(5, 5)
    )
    for (x, y, w, h) in faces:
        cv2.circle(frame, (int((x + x + w) / 2), int((y + y + h) / 2)), int(w / 2), (0, 255, 0), 3)

    cv2.imshow('frame', frame)   # 显示
    cv2.waitKey(1)

# 退出
cap.release()
cv2.destroyAllWindows()





